package com.raisecom.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * @author lilz-2686
 *
 */
public class DomTest {
	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document document = db.parse(new File("E:/workspace3.7/test/src/student.xml"));
		
		NodeList list = document.getElementsByTagName("student");
		for (int i = 0; i < list.getLength(); i++) {
			Element element = (Element) list.item(i);
			String content = element.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
			System.out.println("name:" + content);
			
			content = element.getElementsByTagName("birthday").item(0).getFirstChild().getNodeValue();
			System.out.println("birthday:" + content);
		}
		
		//��ȡԪ�صĸ�Ԫ�����
		Element root = document.getDocumentElement();
		System.out.println(root.getTagName());
		
		NodeList lists = root.getChildNodes();
		System.out.println(lists.getLength());
		
		for (int i = 0; i < list.getLength(); i++) {
			System.out.println(list.item(i).getNodeName());
		}
		
		System.out.println("-----------------------------------");
		
		for (int i = 0; i < list.getLength(); i++) {
			Node n = list.item(i);
			System.out.println(n.getNodeType() + ":" + n.getNodeValue());
		}
		
		System.out.println("------------------------------------");
		
		for (int i = 0; i < list.getLength(); i++) {
			Node n = list.item(i);
			System.out.println(n.getTextContent());
		}
		
		System.out.println("------------------------------------");
		
		NodeList nodeList = document.getElementsByTagName("student");
		
		for (int i = 0; i < list.getLength(); i++) {
			NamedNodeMap nnm = nodeList.item(i).getAttributes();
			
			String attrName = nnm.item(0).getNodeName();
			System.out.println(attrName);
			
			System.out.println("=");
			
			String attrValue = nnm.item(0).getNodeValue();
			System.out.println(attrValue);
		}
		
		
	}
}
























